package com.dmj.sqldsl.builder;

import com.dmj.sqldsl.builder.condition.ConditionsBuilder;
import com.dmj.sqldsl.builder.config.EntityConfig;
import com.dmj.sqldsl.builder.config.GlobalConfig;
import com.dmj.sqldsl.builder.table.EntityBuilder;
import com.dmj.sqldsl.model.DslDelete;
import com.dmj.sqldsl.model.Where;

public class DeleteBuilder {

  private final EntityBuilder entityBuilder;

  private ConditionsBuilder whereConditions;

  private DeleteBuilder(EntityBuilder entityBuilder) {
    this.entityBuilder = entityBuilder;
  }

  public static DeleteBuilder from(Class<?> entityClass) {
    return new DeleteBuilder(new EntityBuilder(entityClass));
  }

  public DeleteBuilder where(ConditionsBuilder conditionsBuilder) {
    this.whereConditions = conditionsBuilder;
    return this;
  }

  public DslDelete toDelete(EntityConfig config) {
    return DslDelete.builder()
        .table(entityBuilder.buildTable(config))
        .where(new Where(whereConditions.build(config)))
        .build();
  }

  public DslDelete toDelete() {
    return this.toDelete(GlobalConfig.getEntityConfig());
  }
}
